package com.sc.passbook.mapper;

import com.sc.passbook.constant.Constant;
import com.sc.passbook.vo.PassTemplate;
import com.spring4all.spring.boot.starter.hbase.api.RowMapper;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * @author sc
 * Created on  2018/9/1
 */
public class PassTemplateMapper implements RowMapper<PassTemplate> {

    private static byte[] FAMILY_B = Constant.PassTemplateTable.FAMILY_B.getBytes();
    private static byte[] ID = Constant.PassTemplateTable.ID.getBytes();
    private static byte[] TITLE = Constant.PassTemplateTable.TITLE.getBytes();
    private static byte[] SUMMARY = Constant.PassTemplateTable.SUMMARY.getBytes();
    private static byte[] DESC = Constant.PassTemplateTable.DESC.getBytes();
    private static byte[] HAS_TOKEN = Constant.PassTemplateTable.HAS_TOKEN.getBytes();
    private static byte[] BACKGROUND = Constant.PassTemplateTable.BACKGROUND.getBytes();
    private static byte[] FAMILY_C = Constant.PassTemplateTable.FAMILY_C.getBytes();
    private static byte[] LIMIT = Constant.PassTemplateTable.LIMIT.getBytes();
    private static byte[] START = Constant.PassTemplateTable.START.getBytes();
    private static byte[] END = Constant.PassTemplateTable.END.getBytes();

    @Override
    public PassTemplate mapRow(Result result, int i) throws Exception {
        PassTemplate passTemplate = new PassTemplate(
                Bytes.toLong(result.getValue(FAMILY_B,ID)),
                Bytes.toString(result.getValue(FAMILY_B,TITLE)),
                Bytes.toString(result.getValue(FAMILY_B,SUMMARY)),
                Bytes.toString(result.getValue(FAMILY_B,DESC)),
                Bytes.toBoolean(result.getValue(FAMILY_B,HAS_TOKEN)),
                Bytes.toInt(result.getValue(FAMILY_B,BACKGROUND)),
                Bytes.toLong(result.getValue(FAMILY_C,LIMIT)),
                DateUtils.parseDate(Bytes.toString(result.getValue(FAMILY_C,START)),"yyyy-MM-dd"),
                DateUtils.parseDate(Bytes.toString(result.getValue(FAMILY_C,END)),"yyyy-MM-dd")
        );
        return passTemplate;
    }
}
